package controlleur;

import exceptions.DataSourceException;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modele.Employe;
import modele.InterfaceEmployeModel;
import org.apache.struts.action.*;

public class UpdateEmployeAction extends GenericAction {

    @Override
    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
            throws IOException,
                   ServletException {
        ActionMessages errors = new ActionMessages();
        ActionForward verif = null;
        String target = "success";

        // Teste sur l'identification de l'utilisateur
        if ((verif = verificationIdentification(mapping, request)) != null)
            return verif;

        // Annulation depuis la liste des employes
        if (isCancelled(request))
            return (mapping.findForward(target));

        // Parse du formulaire en employe
        Employe employe = getEmploye(form);

        try {
            InterfaceEmployeModel employeModel = getModel(request);
            employeModel.update(employe);
        } catch (DataSourceException e) {
            target = "error";
            errors.add(ActionMessages.GLOBAL_MESSAGE,
                       new ActionMessage("errors.database.error"));
        }

        // Signaler les erreurs eventuelles
        if (!errors.isEmpty())
            saveErrors(request, errors);

        // Transmettre la requete a la vue appropriee
        return (mapping.findForward(target));
    }
}